package com.lynn.salary.web.action;

import java.util.List;
import java.util.Map;

import com.lynn.salary.entity.Permission;
import com.lynn.salary.entity.Role;
import com.lynn.salary.pojo.FeedBack;
import com.lynn.salary.service.IRolePermissionService;
import com.lynn.salary.util.ObjectFactory;

public class RolePermissionAction extends BaseAction {

	private List<Map<String, Object>> permissionList;
	private FeedBack feedBack;
	private String flag;
	private Permission permission;
	private String[] ids;
	private List<Map<String, Object>> roleList;
	private Role role;
	
	private IRolePermissionService rolePermissionService = (IRolePermissionService) ObjectFactory
			.getBean("rolePermissionService");

	  
	  public String deleteR() throws Exception {
			
			if(ids != null) {
				rolePermissionService.deleteRole(ids);
			} else {
				rolePermissionService.deleteRole(deleteId);
			}
			feedBack = new FeedBack();
			feedBack.setStatusCode("200");
			feedBack.setMessage("删除成功");
			feedBack.setRel("roleMgPage");
			feedBack.setNavTabId("roleMgPage");
			feedBack.setForwardUrl("");
			feedBack.setCallbackType("");
			return "deleteSuccess";
		}
	  public String editR() throws Exception {
			if (flag.equals("showEditRolePage")) {
				role = rolePermissionService.findRoleById(editId);
				return "showEditRolePage";
			} else if (flag.equals("update")) {
				rolePermissionService.updateRole(role);
				feedBack = new FeedBack();
				feedBack.setStatusCode("200");
				feedBack.setMessage("更新成功");
				feedBack.setRel("roleMgPage");
				feedBack.setNavTabId("roleMgPage");
				feedBack.setForwardUrl("");
				feedBack.setCallbackType("closeCurrent");
				return "editSuccess";
			}
			return "error";
		}
	
	  public String execute() throws Exception {
		totalCount = rolePermissionService.totalCount();
		permissionList = rolePermissionService.findPermissionByLimit(pageNum,
				numPerPage);
		this.currentPage = pageNum;
		return SUCCESS;
	}
	
	public String saveR() throws Exception {
		rolePermissionService.save(role);

		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		feedBack.setMessage("添加成功");
		feedBack.setRel("roleMgPage");
		feedBack.setNavTabId("roleMgPage");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("closeCurrent");
		return "saveSuccess";
	}

	public String save() throws Exception {
		rolePermissionService.save(permission);

		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		feedBack.setMessage("添加成功");
		feedBack.setRel("permissionMgPage");
		feedBack.setNavTabId("permissionMgPage");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("closeCurrent");
		return "saveSuccess";
	}

	public String delete() throws Exception {
		
		if(ids != null) {
			rolePermissionService.delete(ids);
		} else {
			rolePermissionService.delete(deleteId);
		}
		
		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		feedBack.setMessage("删除成功");
		feedBack.setRel("permissionMgPage");
		feedBack.setNavTabId("permissionMgPage");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("");
		return "deleteSuccess";
	   }
       

	public String edit() throws Exception {
		if (flag.equals("showEditPage")) {
			permission = rolePermissionService.findPermissionById(editId);
			return "showEditPage";
		} else if (flag.equals("update")) {
			rolePermissionService.updatePermission(permission);
			feedBack = new FeedBack();
			feedBack.setStatusCode("200");
			feedBack.setMessage("更新成功");
			feedBack.setRel("permissionMgPage");
			feedBack.setNavTabId("permissionMgPage");
			feedBack.setForwardUrl("");
			feedBack.setCallbackType("closeCurrent");
			return "editSuccess";
		}
		return "error";
	}

	public List<Map<String, Object>> getPermissionList() {
		return permissionList;
	}

	public void setPermissionList(List<Map<String, Object>> permissionList) {
		this.permissionList = permissionList;
	}

	public FeedBack getFeedBack() {
		return feedBack;
	}

	public void setFeedBack(FeedBack feedBack) {
		this.feedBack = feedBack;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public Permission getPermission() {
		return permission;
	}

	public void setPermission(Permission permission) {
		this.permission = permission;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public List<Map<String, Object>> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Map<String, Object>> roleList) {
		this.roleList = roleList;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}
    
}
